<?php

namespace App\Http\Resources;

use App\Models\CustomerModel;
use App\Models\GoldModel;
use Illuminate\Http\Resources\Json\JsonResource;

class UserModelResource extends JsonResource
{
    /**
     * @param $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            'id'            => $this->id,
            'openid'        => $this->user_openid,
            'uuid'          => $this->uuid,
            'username'      => $this->username,
            'avatar'        => $this->avatar,
            'mobile'        => $this->mobile,
            'charge_total'  => CustomerModel::where('Account', $this->username)->value('Balance'),
            'gold'          => GoldModel::where('uid', $this->id)->where('status', 1)->sum('gold'),
            'access_token'  => $this->access_token,
            'permissions'   => array_filter(explode(',', $this->role)),
        ];
    }
}
